//
//  LLT_NewsViewController_LXH.m
//  CoderLLT
//
//  Created by 刘晓萌 on 15/12/12.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//
#import "LLT_LoginViewController_LXM.h"
#import "LLT_NewsViewController_LXH.h"
#import "LLT_NewsDetailViewController_LXH.h"
#import "LLT_NewsTableViewCell_LXH.h"
#import "LLT_NewsModel.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "LLT_DataSender_LXH.h"
#import "LLT_CollectViewController_LXM.h"
#import "LLT_NavigationViewController_LXM.h"
#import "UIViewController+LLT_LoginAndRegist_LXM.h"
#import <AVOSCloud/AVOSCloud.h>


@interface LLT_NewsViewController_LXH ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *LLT_NewsTableView;

@property (nonatomic, strong) NSMutableArray *newsArray;

@property (nonatomic, strong) MJRefreshNormalHeader *refreshHeader;

@property (nonatomic, strong) MJRefreshBackNormalFooter *refreshFooter;


@end

@implementation LLT_NewsViewController_LXH

#pragma mark --- dealloc

-(void)dealloc
{
    [self.LLT_NewsTableView removeFromSuperview];
    self.LLT_NewsTableView = nil;
    self.LLT_NewsTableView.delegate  = nil;
    self.LLT_NewsTableView.dataSource = nil;
    self.newsArray = nil;
    self.refreshHeader = nil;;
    self.refreshFooter = nil;
}



#pragma mark - set MJRefresh

/**
 * 下拉刷新
 */
-(MJRefreshNormalHeader *)refreshHeader{
    
    __weak typeof(self.LLT_NewsTableView) myTableView = self.LLT_NewsTableView;
    
    __weak typeof(self) weakSelf = self;
    
    if (!_refreshHeader) {
        
        _refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            weakSelf.newsArray = [NSMutableArray new];
            
            [weakSelf makeDataWithCounts:weakSelf.newsArray.count];
            
            [myTableView reloadData];
            
            [_refreshHeader endRefreshing];
            
        }];
    }

    return _refreshHeader;
    
}

/**
 *上拉加载
 */
-(MJRefreshBackNormalFooter *)refreshFooter{
    
    if (!_refreshFooter) {
        
        _refreshFooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            
            [self makeDataWithCounts:self.newsArray.count];
            
            [_refreshFooter endRefreshing];
            
        }];
    }
    
    return _refreshFooter;
    
}

#pragma mark --- 懒加载

/**
 * 懒加载
 */
-(NSMutableArray *)newsArray{
    if (!_newsArray) {
        
        _newsArray = [NSMutableArray new];
        
    }
    
    return _newsArray;
    
}

#pragma mark --- 加载视图

- (void)viewDidLoad {
    [super viewDidLoad];

    [self makeTabbar];
    
    [self makeTableView];
    
    [self makeDataWithCounts:self.newsArray.count];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}


/**
 * 数组里数据的个数
 */
-(NSInteger)countOfNewsArray{
    
    return self.newsArray.count;
    
}

/**
 * 获取到tableView得数据
 */
-(void)makeDataWithCounts:(NSInteger)count{
    
    __weak typeof(self.LLT_NewsTableView) myTableView = self.LLT_NewsTableView;
    
    __weak typeof(self) weakSelf = self;
    
    LLT_DataSender_LXH *dataSender = [[LLT_DataSender_LXH alloc]init];
    
    [dataSender getNewsArrayByCount:count];
    
    dataSender.sendNewsArray = ^(NSMutableArray *newsArray)
    {
        [weakSelf.newsArray addObjectsFromArray:newsArray];
        
        [myTableView reloadData];
    };
   
}

/**
 * 设置tabbar
 */
-(void)makeTabbar{
    self.tabBarController.tabBar.tintColor = [UIColor colorWithRed:255.0/255.0 green:124.0/255.0 blue:56.0/255.0 alpha:1];
}


/**
 * 设置tableView
 */
-(void)makeTableView{
    
    self.LLT_NewsTableView.delegate = self;
    
    self.LLT_NewsTableView.dataSource = self;
    
    [self.LLT_NewsTableView registerNib:[UINib nibWithNibName:@"LLT_NewsTableViewCell_LXH" bundle:nil] forCellReuseIdentifier:@"News_cell"];
    
    [self.LLT_NewsTableView addSubview:self.refreshHeader];
    
    [self.LLT_NewsTableView addSubview:self.refreshFooter];
    
}

/**
 * 电池栏 文字变白
 */
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self countOfNewsArray];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LLT_NewsTableViewCell_LXH *cell = [tableView dequeueReusableCellWithIdentifier:@"News_cell" forIndexPath:indexPath];
    
    cell.newsModel = self.newsArray[indexPath.row];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LLT_NewsModel *news = self.newsArray[indexPath.row];
    
    LLT_NewsDetailViewController_LXH *newsDetailViewController = [MainStoryBoard instantiateViewControllerWithIdentifier:@"newsDetail"];
    
    newsDetailViewController.urlString = news.newsid;
    
    newsDetailViewController.newsModel = news;
    
    [self presentViewController:newsDetailViewController animated:YES completion:nil];
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(15, 0, self.view.frame.size.width - 15, 20)];
    UILabel *label = [[UILabel alloc]initWithFrame:view.frame];
    label.text = @"新闻列表";
    label.font = [UIFont systemFontOfSize:11];
    [view addSubview:label];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (IBAction)didClickSaveButton:(UIButton *)sender {
    __weak typeof(self) weakSelf = self;
    if ([AVUser currentUser].mobilePhoneNumber.length <= 0) {
        [weakSelf showAlert:@"提示" andAlertString:@"当前游客账号尚未注册登录,是否要返回登录页面" andHandler:^(UIAlertAction *action) {
            LLT_LoginViewController_LXM *lvc = getVC(@"loginVC");
            [weakSelf logout];
            [weakSelf showViewController:lvc sender:nil];
        }];
        return;
    }
    [self presentViewController:[MainStoryBoard instantiateViewControllerWithIdentifier:@"colloctionView"] animated:YES completion:nil];
}
/**
 * 登出功能
 */
-(void)logout{
    [self dismissViewControllerAnimated:YES completion:nil];
    //更改登录状态为NO
    NSUserDefaults *loginDefault = [NSUserDefaults standardUserDefaults];
    [loginDefault setBool:NO forKey:@"online"];
    
    [self presentViewController:getVC(@"loginVC") animated:YES completion:nil];
}

/**
 * 添加复杂点儿allert提示
 */
-(void)showAlert:(NSString *)alertTitle andAlertString:(NSString *)alertContent andHandler:(void (^ __nullable)(UIAlertAction *action))handler{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:alertTitle message:alertContent preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:handler]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alertVC animated:YES completion:nil];
}



/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


#pragma mark - Navigation
/*
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    NSIndexPath *indexPath = [self.LLT_NewsTableView indexPathForSelectedRow];
    
    LLT_NewsModel *news = self.newsArray[indexPath.row];
    
    
    LLT_NewsDetailViewController_LXH *newsDetailViewController = [segue destinationViewController];
    
    newsDetailViewController.urlString = news.newsid;
    
    newsDetailViewController.newsModel = self.newsArray[indexPath.row];
}
*/

@end